Show Number Of Comments In WordPress

How To Show Number of Comments In WordPress Theme?

You may have seen that under the post title, the number of comments on each post will be showing. Do you know how to show number of comments in WordPress?

You have to create a function to show the number of comments in WordPress theme so that if your blog post has any comment then it will show the number otherwise, a phrase.

So WordPress themes show the number of comments below the post title, some show it under the excerpt of the post. It’s up to the developer.

If you’re developing a WordPress theme then you can use the WordPress function anywhere you want. In this tutorial, I am going to walk you through the codes which can be used to show number of comments in WordPress homepage or blog page.

Create A Function To Show Number of Comments In WordPress Theme.

First of all, you should know how to add comment box in WordPress theme. It’s because how would you show the comments if you won’t have the comment box? Apart from that, you should also know how to design comment box.

You have to create a function with the proper conditions. The conditions would handle the output. What if your post doesn’t have any comment, what would you want to display?

If you have one comment or greater than that, then what? Do you want someone to reach out to the comment section when they click on the number of comments’ link?

The function would handle everything. It’s up to you, what kind of output do you want?

Let me begin with the codes.

As you know that the WordPress theme file structure consists the functions.php file which handles the functions and the theme support feature generation codes.

Step 1:- You have to add the code written below in this file.

function blogginglove_comment_number() {

$comments_num = get_comments_number();

if (comments_open() ){

if( $comments_num == 0 ){

$comments = __(‘Leave a comment’);

}
elseif ( $comments_num > 1 ){

$comments = $comments_num . __(‘ Comments’);

}
else{

$comments = __(‘Leave a comment’);

}

$comments = ‘<a href=”‘ . get_comments_link() . ‘” class=”comment-link”>’. $comments .'</a>’;

}
else{

$comments = __( ‘Comments are closed’);

}

return $comments;

}

This function would add the feature in your WordPress theme to show comments in WordPress blog if you call the count function of the WordPress.

If your posts don’t have any comment then there would be a phrase “Leave a comment” so that people can click on it. When they click, they would reach directly to the comment box.

Step 2:- Adding the above code won’t show number of comments, you have to call this function to display its output.

If you want to show number of comments under the blog posts’ title then you can use the function within the paragraph where you add author name and date under post title.

If you want to show the comments’ number on the blog page then you have to edit the index.php file of your theme because it handles the blog page.

If you have created a different homepage then you can edit the front-page.php file. It depends on the structure of the theme you are using.

<?php echo blogginglove_comment_number(); ?>

Here we are calling the function which has been added in the functions.php file. As you can match the name of the function in both the codes is the same.

If you want to use the WordPress default function then you can use it. But for that, you have to create a new loop and use that function inside the loop.

Though your task of adding the comment’s number would accomplish with the codes shown above but still, you should understand that WordPress has many ways to do the same task.

It would depend on the habits of the developer.

Have You Added the Code To Show Number of Comments In WordPress?

You must understand the concept of theme development and then go further with the codes. Don’t just jump into the theme development process. Try to take a little step and understand about each and every file.

If you see the code of two or more WordPress themes, you won’t find the code as same as any other theme. It’s because every developer has his/her own way of coding.

To show number of comments in WordPress, you can use the codes mentioned above. But make sure that you place the codes at the right place.

by Ravi Chahar

A WordPress Professional and the LinkedIn Influencer. A coder by passion and a blogger by choice. WordPress theme development is his forte. He is your WordPress guy who will teach you how to solve WordPress errors, WordPress security issues, design issues and what not.


Get Free Updates Into Your Inbox

Learn Everything Just Like I Did

SUBSCRIBE



12 comments

  1. Hi Ravi,

    I did not add comment code to my bespoke theme but did take it out LOL ? I closed comments for a while on my blog. I simply didn’t dig the No Comments look that popped up by closing comments so I removed the code from my theme entirely. Sure I mucked things up. Like a dingbat ? But now we’re back in order. Super key to have comment numbers working for you too. Gotta get engagement going and when folks see high volume numbers you just know they want to join the party and get things cracking.

    Thanks for sharing Ravi ?

    Ryan

    1. Hey Ryan,

      It’s the matter of choice. I have noticed many blogs which are not allowing anyone to drop even a single line. If you mismatch with the theme code then it may turn out to be scary.

      Showing the number of comments is like inviting others to join the party. I visited your blog when you decided to disable the comments. Even I thought about it once. But I didn’t find it a good idea.

      Thanks for taking the time to share your thoughts.

      Have a wonderful day.

      ~Ravi

  2. Hi Ravi,

    I personally think that it’s a really good idea to show the number of comments below the post title. It shows the level of engagement on that particular post and rather ‘lures’ readers to read and drop a comment themselves.

    Again, very simple yet effective explanation on how to achieve this. Thanks for sharing Ravi. Have a good one!

    1. Hey Arpit,

      If you’re not going to show the number of comments then how would your readers know about the engagement? You are totally right about it. But when it comes to the development part then most of the people fret a lot.

      So I have provided an easy guide for them. Even if your WordPress theme doesn’t have this features, you can add it.

      Thanks for sharing your words.

      Have a great week ahead.

      ~Ravi

  3. Hi Ravi,

    Very important part of a blog for me. I like to look at the top of the post and see how many comments there are. Of course, not everyone (like Ryan) doesn’t like that number there. To each his/her own, right? Great tutorial, though. Very impressive!

    B

    1. Hey Bren,

      As I told Ryan, it depends person to person. Some people prefer to show the number of comments, others don’t. I just wanted to provide the best solution for the people who want to display it.

      Thanks for stopping by.

      Have a great day.

      ~Ravi

    1. Hey Edwin,

      There is no specific plugin for that. It’s because the number of comments can only be shown if you have added the comment box in your WordPress theme. Though you can show the total number of comments, top comments, etc using plugins but still, no specific plugin which can work like the code.

      Thanks for stopping by.

      ~Ravi

  4. Hi Ravi

    I too like to see the number of comments on a post. Sometimes I enjoy reading people’s comments as much as I enjoy the original post.

    Joy – Blogging After Dark

    1. Hey Joy,

      Sometimes it’s fun to read the comments. Though the display feature of the comments number may vary from one website to another. But still, the required method is shown in the tutorial.

      Thanks for sharing.

      Enjoy the day.

      ~Ravi

  5. Hi Ravi,

    This is really a new thing to me.

    I used a plugin for showing my top comment authors on my page.

    I think in that plugin there are options to show comment number and the post url or headline of the post etc.

    Since I have not much knowledge on these technical aspects i opt out generally but surely depend on plugin which is safe. ?

    Anyways its really good to be hear again to read this tuto.

    Thanks for sharing

    Have a great day

    Best

    ~ Philip

    1. Hey Philip,

      Most of the WordPress users prefer to use the plugins. But if you have a better understanding of codes then showing the number of costs can be really easy.

      It would depend on the matter of choice. But in a WordPress theme, if you want to show the comments’ count along with every post, you have to use the codes.

      Thanks for your contribution.

      Enjoy your day.

      ~Ravi

Leave a Reply

Your email address will not be published. Required fields are marked *